package com.infoland.afanmall.model.story.adapter;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.model.story.activity.StoryInfoActivity;
import com.infoland.afanmall.model.story.entity.StoryInfoAdapterEntity;
import com.infoland.afanmall.model.story.entity.StoryInfoEntity;
import com.infoland.afanmall.model.youXuan.activity.GoodInfoActivity;
import com.wyj.cardtantan.CardItemTouchHelperCallback;
import com.wyj.cardtantan.CardLayoutManager;
import com.wyj.cardtantan.OnSwipeListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 王永吉
 * @date 2018/8/13 12:49
 * StoryInfoAdapter 故事详情 中间的适配器
 */
public class StoryInfoAdapter extends BaseMultiItemQuickAdapter<StoryInfoAdapterEntity, BaseViewHolder> {
    private int goodId;
    private StoryInfoActivity storyInfoActivity;

    public StoryInfoAdapter(List<StoryInfoAdapterEntity> data) {
        super(data);
        addItemType(0, R.layout.story_recycle_text);
        addItemType(1, R.layout.story_recycle_image);
        addItemType(2, R.layout.story_recycle_images);
        addItemType(3, R.layout.story_recycle_video);
        addItemType(9, R.layout.story_recycler_share);
    }

    @Override
    protected void convert(BaseViewHolder helper, StoryInfoAdapterEntity item) {
        switch (helper.getItemViewType()) {
            /*** text**/
            case 0:
                ToolsKit.setText(helper.getView(R.id.story_text), item.getText());
                break;
            /*** image**/
            case 1:
                ToolsKit.setImgView(helper.getView(R.id.story_image), item.getImage().getUrl(), helper.getView(R.id.story_image));
                break;
            /*** images**/
            case 2:
                ArrayList<String> urls = new ArrayList<>();
                if (item.getImages().size() > 0) {
                    for (StoryInfoEntity.ObjectBean.SContextBean.ImagesBean imagesBean : item.getImages()) {
                        urls.add(imagesBean.getUrl());
                    }
                }
                setImageCards(helper.getView(R.id.recycler_images), urls);
                break;
            /*** video**/
            case 3:
                ToolsKit.setVideoImg(helper.getView(R.id.story_video), item.getVideo().getI_url());
                ToolsKit.setVideo(helper.getView(R.id.story_video), item.getVideo().getV_url());
                break;
            /*** 分享**/
            case 9:
                if (storyInfoActivity != null) {
                    helper.getView(R.id.story_info_share).setOnClickListener(v -> {
                        ToolsKit.openShareBottom(storyInfoActivity);
                    });
                }
                break;
            default:
                break;
        }
    }

    private void setImageCards(RecyclerView recycler, ArrayList<String> urls) {
        recycler.setItemAnimator(new DefaultItemAnimator());
        ImageCardAdapter myAdapter = new ImageCardAdapter(R.layout.story_card, urls);
        recycler.setAdapter(myAdapter);
        CardItemTouchHelperCallback cardCallback = new CardItemTouchHelperCallback(myAdapter, urls);
        cardCallback.setOnSwipedListener(new OnSwipeListener<String>() {

            @Override
            public void onSwiping(RecyclerView.ViewHolder viewHolder, float ratio, int direction) {
//                ImageCardAdapter.MyViewHolder myHolder = (ImageCardAdapter.MyViewHolder) viewHolder;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, String o, int direction) {
//                ImageCardAdapter.MyViewHolder myHolder = (ImageCardAdapter.MyViewHolder) viewHolder;
//                Toast.makeText(MainActivity.this, direction == CardConfig.SWIPED_LEFT ? "swiped left" : "swiped right", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSwipedClear() {
//                recyclerView.postDelayed(() -> {
//                    initData();
//                    recyclerView.getAdapter().notifyDataSetChanged();
//                }, 3000L);
            }
        });
        final ItemTouchHelper touchHelper = new ItemTouchHelper(cardCallback);
        final CardLayoutManager cardLayoutManager = new CardLayoutManager(recycler, touchHelper);
        recycler.setLayoutManager(cardLayoutManager);
        touchHelper.attachToRecyclerView(recycler);
    }

    public void setGoodId(int goodId) {
        this.goodId = goodId;
    }

    public void setStoryInfoActivity(StoryInfoActivity storyInfoActivity) {
        this.storyInfoActivity = storyInfoActivity;
    }

}
